package com.ruoyi.threads;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @Author syc
 * @Date Created in 14:31 2022/8/10
 * @Description
 */
// 练习Thread. 实现多线程同步下载图片
public class TestThread2 extends Thread {

    private String url; // 网络图片地址
    private String name; // 保存的文件名
    public TestThread2 (String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownlocader webDownlocader = new WebDownlocader();
        webDownlocader.downloader(url ,name);
        System.out.println("下载了文件名为:" + name);
    }

    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://img14.360buyimg.com/seckillcms/s280x280_jfs/t1/108555/35/19907/134169/62ec7d33E7dd4c66a/319676528abf3745.jpg", "D:/image/4.jpg");
        TestThread2 t2 = new TestThread2("https://img12.360buyimg.com/seckillcms/s280x280_jfs/t1/211304/34/15771/49196/61dfc5b6Ea71b446b/cd9686a1c4bf155c.jpg", "D:/image/5.jpg");
        TestThread2 t3 = new TestThread2("https://img10.360buyimg.com/seckillcms/s280x280_jfs/t1/167162/19/26045/208075/62ea1314Ee4310813/460f1f5c4979033c.jpg", "D:/image/6.jpg");
        t1.start();
        t2.start();
        t3.start();
    }



}

// 下载器
class WebDownlocader {
    // 下载方法
    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现问题");
        }
    }



}


